package com.puman.beautyfarmcounselor.ui.customer;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.puman.beautyfarmcounselor.IndexActivity;
import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.http.AjaxCallBack;
import com.puman.beautyfarmcounselor.ui.core.BaseFragment;
import com.puman.beautyfarmcounselor.ui.core.BaseInferface;
import com.puman.beautyfarmcounselor.ui.customer.model.LableData;
import com.puman.beautyfarmcounselor.ui.home.model.CustomerDetail;
import com.puman.beautyfarmcounselor.util.MethedUtil;
import com.puman.beautyfarmcounselor.util.XTHttp;
import com.puman.beautyfarmcounselor.util.config.GlobalData;
import com.puman.beautyfarmcounselor.util.config.RequestAddr;
import com.puman.beautyfarmcounselor.util.view.XCFlowLayout;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 */
@EFragment(R.layout.fragment_lable)
public class LableFragment extends BaseFragment implements BaseInferface {


    @ViewById(R.id.back_btn)
    ImageView backBtn;
    @ViewById(R.id.base_title)
    TextView title;

    @FragmentArg
    String lableList;

    @ViewById(R.id.flowlayout)
    XCFlowLayout mFlowLayout;
    @ViewById(R.id.flowlayout_lib)
    XCFlowLayout mFlowLayoutLib;

    String[] lables;

    List<String> cusLableList = new ArrayList<>();
    List<String> libLableList = new ArrayList<>();

    @Override
    @AfterViews
    public void updateView() {
        title.setText(R.string.detailLable);
        backBtn.setVisibility(View.VISIBLE);

        getData();

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.leftMargin = 10;
        layoutParams.rightMargin = 10;
        layoutParams.topMargin = 5;
        layoutParams.bottomMargin = 5;
        lables = lableList.split(",");
        cusLableList = Arrays.asList(lables);
//        for (int i = 0; i < cusLableList.size(); i++) {
//            final TextView view = new TextView(getActivity());
//            view.setText(cusLableList.get(i));
//            view.setTextColor(getActivity().getResources().getColor(R.color.themeColor));
//            view.setBackground(getActivity().getResources().getDrawable(R.drawable.circle_white_bg_gray_bound));
//            view.setTag(i);
//            view.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Toast.makeText(getActivity(), "onclick:"+v.getTag()+","+view.getText(), Toast.LENGTH_SHORT).show();
//                }
//            });
//            mFlowLayout.addView(view, layoutParams);
//        }
        mFlowLayout.setLayoutParams(layoutParams);
        setFlowLayout(mFlowLayout, cusLableList, 1);
    }

    XTHttp xtHttp = new XTHttp();
    String url = "";
    String param = "";

    private void getData() {
        param = "emp_id=" + GlobalData.counselor.getData().getEmployee_id();
        url = RequestAddr.lableLib + param;
        xtHttp.get(url, new AjaxCallBack<Object>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                dealData(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(IndexActivity.getInstace(), "网络不稳定，请稍后再试", Toast.LENGTH_SHORT).show();
                super.onFailure(t, errorNo, strMsg);
//                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    LableData lableData;

    private void dealData(Object o) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            lableData = mapper.readValue(o.toString(), LableData.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (lableData == null)
            return;


        Arrays.toString(lables);
        MethedUtil.UtilLog("lables:" + Arrays.toString(lables));
//        lableList =  customerDetail.getData().getLabels();
        for (int i = 0; i < lableData.getData().size(); i++) {
            libLableList.add(lableData.getData().get(i).getName());
        }

        setFlowLayout(mFlowLayoutLib, libLableList, 2);
    }

    /**
     * 设置lable
     *
     * @param flowLayout
     * @param lableList
     * @param flag       1:cus  2:lib
     */
    private void setFlowLayout(final XCFlowLayout flowLayout, List<String> lableList, final int flag) {
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.leftMargin = 10;
        layoutParams.rightMargin = 10;
        layoutParams.topMargin = 5;
        layoutParams.bottomMargin = 5;
        flowLayout.removeAllViews();
        for (int i = 0; i < lableList.size(); i++) {
            TextView view = new TextView(getActivity());
            view.setText(lableList.get(i));
            if (flag == 1) {
                view.setTextColor(getActivity().getResources().getColor(R.color.themeColor));
                view.setBackground(getActivity().getResources().getDrawable(R.drawable.circle_white_bg_theme_bound));
            } else {
                view.setTextColor(getActivity().getResources().getColor(R.color.gray));
                view.setBackground(getActivity().getResources().getDrawable(R.drawable.circle_white_bg_gray_bound));
            }

            view.setTag(i);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    MethedUtil.UtilToast(getActivity(),"onclick:"+v.getTag());
//                    if (flag == 1) {
//                            cusLableList.remove(v.getTag());
//                        setFlowLayout(mFlowLayout, cusLableList, 1);
//                    } else {
//                        cusLableList.add(libLableList.get((int)v.getTag()));
//                        setFlowLayout(mFlowLayout, cusLableList, 1);
//                    }

                }
            });
            flowLayout.addView(view, layoutParams);
        }
        flowLayout.setLayoutParams(layoutParams);

    }

    public int index = 0x01;
    public int status = 0x02;//0:unselected  1:selected


    @Override
    @Click(R.id.back_btn)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstace().getSupportFragmentManager().popBackStack();
                break;
        }
    }
}
